<?php

class Cache
{

	/**
	 * @var array cache instance
	 */
	static protected $_inst = array();

	/**
	 * @var array
	 */
	protected $_config = array();

	/**
	 * name of offset configuration
	 * @var string
	 */
	protected $_name = null;

	/**
	 * to string method
	 * @return string
	 */
	public function __toString()
	{
		return $this -> _name;
	}

	/**
	 * constructor
	 */
	protected function __construct($name, $config)
	{

		$this -> _config = $config;

		$this -> _name = $name;

		self::$_inst[$name] = $this;
	}

	/**
	 * @param string $id
	 * @return string
	 */
	public function _sanitizeId($id)
	{
		return $id;
	}

	/**
	 * @param string $name
	 * @param mixed $default
	 * @return mixed
	 */
	public function getConfig($key, $default = NULL)
	{
		return isset($this -> _config[$key]) ? $this -> _config[$key] : $default;
	}

	static public function inst($name = NULL)
	{
		if (NULL == $name)
		{
			$name = 'default';
		}

		if (!isset(self::$_inst[$name]))
		{
			$config = Agilet::loadConfig('cache.php',$name);
			$driver = $config['driver'];
			$class = 'Cache_' . ucfirst($driver);
			return new $class($name, $config);
		}
		return self::$_inst[$name];
	}

}
